﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAL;
using HomeStayWebCP.Models;
using HomeStayWebCP.Models.BusinessObject;

namespace HomeStayWebCP.Controllers
{
    public class CommentsController : Controller
    {
        //
        // GET: /Comments/

        #region Hoangdx - Code Comment
        //
        // GET: /Comments/

        public ActionResult Index()
        {
           
                int postID = (int.Parse(HttpContext.Session["PostID"].ToString()));
            
           
            List<CommentObject> ListCommentView = new List<CommentObject>();
            CommentModel model = new CommentModel();
            ListCommentView = model.GetCommentOfPost(postID);
            ViewBag.ListComment = ListCommentView;

            return View();
        }

        public ActionResult ListCommentView()
        {
            int postID = (int.Parse(HttpContext.Session["PostID"].ToString()));
            List<CommentObject> ListCommentView = new List<CommentObject>();
            CommentModel model = new CommentModel();
            ListCommentView = model.GetCommentOfPost(postID);
            return PartialView(ListCommentView);
        }

        [HttpPost]
        public ActionResult InsertComment(CommentObject commentsDTO)
        {
            List<CommentObject> commentModel = new List<CommentObject>();
            string Username = HttpContext.Session["username"].ToString();
            int postID = (int.Parse(HttpContext.Session["PostID"].ToString()));
            commentsDTO.Content = commentsDTO.Content.Replace("\n",System.Environment.NewLine);
            commentsDTO.UserName = Username;
            commentsDTO.PostID = postID;

            CommentModel bModel = new CommentModel();
            bModel.InsertComments(commentsDTO);

            //Thuc Hien dung tung step : step 1 --> step 2

            CommentModel model = new CommentModel();
            string id = null;
            int count = model.SetCountPaging(5, postID);//Step1: Get so page 
            ViewBag.ListComment = model.GetCommentOfPaing(ref id, postID);//Step 2: get data cho moi page

            //ViewBag.data = "abc";
            ViewBag.count = count;
            ViewBag.page = id;

            return PartialView("Index");
        }

        [HttpGet]
        public ActionResult Paging(String id)
        {
            //Thuc Hien dung tung step : step 1 --> step 2
            int postID = (int.Parse(HttpContext.Session["PostID"].ToString()));
            CommentModel model = new CommentModel();
            int count = model.SetCountPaging(5, postID);//Step1: Get so page 
            ViewBag.ListComment = model.GetCommentOfPaing(ref id, postID);//Step 2: get data cho moi page

            //ViewBag.data = "abc";
            ViewBag.count = count;
            ViewBag.page = id;

            if (Request.IsAjaxRequest())
            {
                return PartialView("Index");
            }
            return View("Index");
        }

        #endregion

        
        public ActionResult DeleteComment(int commentId)
        {
            int postID = (int.Parse(HttpContext.Session["PostID"].ToString()));
            CommentModel model = new CommentModel();
            model.DeleteComment(commentId);
            string id = null;
            int count = model.SetCountPaging(5, postID);//Step1: Get so page 
            ViewBag.ListComment = model.GetCommentOfPaing(ref id, postID);//Step 2: get data cho moi page

            //ViewBag.data = "abc";
            ViewBag.count = count;
            ViewBag.page = id;
            return RedirectToAction("ManViewPost","ManagePosts",new {postID = postID});
        }
    }
}
